package com.zhiyou100.tool;

import java.sql.*;

/**
 * @packageName: Tools
 * @className: com.zhiyou100.tool
 * @Description: TODO 工具类
 * @author: yang
 * @date: 7/20/20
 */
public class Tools {
    /**
     * Loading class `com.mysql.jdbc.Driver'.
     * This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'.
     * The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
     */
    private static final String DRIVER_CLASS = "com.mysql.cj.jdbc.Driver";

    private static final String IP = "192.168.217.82";
    private static final String PORT = "3306";
    private static final String DATABASE_NAME = "db_26";
    private static final String USER_NAME = "root";
    private static final String USER_PASSWORD = "1";
    private static final String URL = "jdbc:mysql://" + IP + ":" + PORT + "/" + DATABASE_NAME;

    static {
        // 一定把驱动加载放在 static 代码块中
        try {
            Class.forName(DRIVER_CLASS);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("加载驱动失败");
        }
    }


    /**
     * @Description: TODO 获取连接
     * @name: getConnection
     * @param: []
     * @return: java.sql.Connection
     * @date: 7/14/20 17:55
     * @author: yang
     */
    public static Connection getConnection() {
        try {
            Connection connection = DriverManager.getConnection(URL, USER_NAME, USER_PASSWORD);
            return connection;
        } catch (SQLException throwable) {
            throw new RuntimeException("获取连接失败");
        }
    }

    /**
     * @Description: TODO 关闭  JDBC
     * @name: close
     * @param: [resultSet, statement, connection]
     * @return: void
     * @date: 7/14/20 19:07
     * @author: yang
     */
    public static void close(ResultSet resultSet, Statement statement, Connection connection) {
        if (resultSet == null) {
            // 如果 resultSet 为 null，关闭 后两个
            close(statement, connection);
            return;
        }
        if (statement == null || connection == null) {
            throw new RuntimeException("statement 或者 connection 为 null");
        }
        try {
            resultSet.close();
        } catch (SQLException throwable) {
            throw new RuntimeException("resultSet 关闭失败");
        }
        try {
            statement.close();
        } catch (SQLException throwable) {
            throw new RuntimeException("statement 关闭失败");
        }
        try {
            connection.close();
        } catch (SQLException throwable) {
            throw new RuntimeException("connection 关闭失败");
        }
    }


    /**
     * @Description: TODO 关闭 JDBC
     * @name: close
     * @param: [statement, connection]
     * @return: void
     * @date: 7/14/20 19:07
     * @author: yang
     */
    public static void close(Statement statement, Connection connection) {
        try {
            statement.close();
        } catch (SQLException throwable) {
            throw new RuntimeException("statement 关闭失败");
        }
        try {
            connection.close();
        } catch (SQLException throwable) {
            throw new RuntimeException("connection 关闭失败");
        }
    }

    /**
     * @name: randomOfMinToMax
     * @param: double min,double max
     * @description: TODO  生成 [min,max]的随机数
     * @date: 2020/2/21 5:17 下午
     * @return: int
     */
    public static int randomOfMinToMax(double min, double max) {
        return (int) (min + (int) (Math.random() * (max - min + 1)));
    }

    public static void main(String[] args) throws SQLException {
        Connection connection = getConnection();
        String sql="select * from Teacher;";
        PreparedStatement statement = connection.prepareStatement(sql);
        ResultSet resultSet = statement.executeQuery();
        while (resultSet.next()){
            String tname = resultSet.getString("tname");
            System.out.println(tname);
        }
        close(resultSet,statement,connection);
    }
}
